import http from '@/utils/http'
import { ApplyModule } from './modules/apply'
export interface ApiResponse<T = any> {
  code: number
  data: T
  errorMsg: string
}

export const getProductList = (): Promise<ApiResponse<ApplyModule.ProductListVo[]>> => {
  return http.get<ApiResponse<ApplyModule.ProductListVo[]>>('/products/list')
}

export const getCurrentAuthInfo = (): Promise<ApiResponse<ApplyModule.UserAuthInfo>> => {
  return http.get<ApiResponse<ApplyModule.UserAuthInfo>>('user/current/auth')
}

/**
 * 用户登录函数
 * @param params - 登录参数对象
 * @param params.mobile - 用户手机号
 * @param params.password - 用户密码
 * @returns 返回一个Promise对象，解析为ApplyModule.UserAuthInfo类型的数据，表示用户的认证信息
 *
 * 此函数通过HTTP POST请求向'/user/login'路径发送登录参数，以获取用户的认证信息。
 */
export const userLogin = (params: {
  mobile: string
  password: string
}): Promise<ApiResponse<ApplyModule.UserAuthInfo>> => {
  return http.post<ApiResponse<ApplyModule.UserAuthInfo>>('/user/login', params)
}

export const userRegister = (params: { password: string; password2:string, mobile: string}) => {
  return http.post<ApplyModule.UserAuthInfo>('/user/register', params)
}

export const resetPassword = (params: { mobile: string; oldPassword: string; newPassword: string }) => {
  return http.post<ApplyModule.UserAuthInfo>('/user/reset/password', params)
}

export const userLogout = () => {
  return http.post('/user/logout')
}

// 获取某个用户的申请记录
export async function getApplyRecords(userId: number): Promise<ApplyModule.ApplyRecord[]> {
  return http.get<ApplyModule.ApplyRecord[]>(`/apply/list/${userId}`)
}

export async function getMyApplyRecords(): Promise<ApiResponse<ApplyModule.ApplyRecord[]>> {
  return http.get<ApiResponse<ApplyModule.ApplyRecord[]>>(`/apply/list`)
}

// 我的 任务列表
export async function searchMyTaskRecords(params: {
  name?: string
  phone?: string
  status?: string
  page: number
  size: number
  dateTime?: string
}): Promise<ApiResponse<ApplyModule.PageList<ApplyModule.TaskVo>>> {
  return http.get<ApiResponse<ApplyModule.PageList<ApplyModule.TaskVo>>>(`/tasks/list`,{
    params,
  })
}


// 新增申请
export const addApplyRecord = (applyData: ApplyModule.AddApplyRecordForm) => {
  return http.post<ApplyModule.AddApplyRecordForm>('/apply/add', applyData)
}

// 修改申请
export const updateApplyRecord = (applyData: ApplyModule.AddApplyRecordForm) => {
  return http.post<ApplyModule.ApplyRecord>('/apply/update', applyData)
}

export const confirmApplyRecord = (data: { id: number }) => {
  return http.post<ApplyModule.ApplyRecord>('/apply/confirm', data)
}

export const sendRegisterCode = (data: { mobile: string }) => {
  return http.post<ApplyModule.ApplyRecord>('/apply/delete', data)
}

export const sendForgotPasswordCode = (data: { mobile: string }) => {
  return http.post<ApplyModule.ApplyRecord>('', data)
}

/**
 * 管理员登录函数
 * @param params - 登录参数对象
 * @param params.username - 用户名
 * @param params.password - 用户密码
 * @returns 返回一个Promise对象，解析为ApplyModule.UserAuthInfo类型的数据，表示用户的认证信息
 *
 *  */
export const adminLogin = (params: {
  username: string
  password: string
}): Promise<ApiResponse<ApplyModule.UserAuthInfo>> => {
  return http.post<ApiResponse<ApplyModule.UserAuthInfo>>('/admin/login', params)
}

/**
 * 管理员端
 */
// 管理员（获取当前登录的用户信息）
export const getAdminAccountInfo = (): Promise<ApiResponse<ApplyModule.UserAuthInfo>> => {
  return http.get<ApiResponse<ApplyModule.UserAuthInfo>>('/admin/current/auth')
}

// 管理员 审核申请
export async function searchApplyRecords(params: {
  name?: string
  phone?: string
  status?: string
  page: number
  size: number
}): Promise<ApiResponse<ApplyModule.PageList<ApplyModule.ApplyRecord>>> {
  return http.get<ApiResponse<ApplyModule.PageList<ApplyModule.ApplyRecord>>>(`/admin/apply/review/list`, {
    params,
  })
}

// 管理员 确认密钥
export async function checkOpCode(params: {opCode: string }) {
  return http.post<ApiResponse<string>>('/admin/check/op/code', params)
}

// 管理员 重置密码
export async function resetUserPwd(params: {mobile:string, newPwd:string, opCode: string }) {
  return http.post<ApiResponse<string>>('/admin/reset/password', params)
}

// 管理员 修改审核状态
export async function modifyApplyRecordStatus(params: { recordId: number; status: string, opCode: string }) {
  return http.post<ApplyModule.ApplyRecord>('/admin/update/apply/status', params)
}

// 管理员 修改期数
export async function modifyApplyRecordWeekNum(params: { recordId: number; weekNum: number, opCode: string }) {
  return http.post<ApplyModule.ApplyRecord>('/admin/update/apply/week', params)
}

// 管理员 修改产品
export async function modifyApplyRecordProduct(params: { recordId: number; limitAmount: number, opCode: string }) {
  return http.post<ApplyModule.ApplyRecord>('/admin/update/apply/product', params)
}

// 管理员 修改备注
export async function modifyApplyRecordRemark(params: { recordId: number; remark: string; opCode: string }) {
  return http.post<ApplyModule.ApplyRecord>('/admin/update/apply/remark', params)
}

// 管理员 修改基础信息
export async function modifyApplyRecordBasicInfo(params: {
  recordId: number
  name: string
  phone: string
  wechat: string
  opCode: string
}) {
  return http.post<ApplyModule.ApplyRecord>('/admin/update/apply/info', params)
}

// 管理员 任务列表
export async function searchTaskRecords(params: {
  name?: string
  phone?: string
  status?: string
  page: number
  size: number
  dateTime?: string
}): Promise<ApiResponse<ApplyModule.PageList<ApplyModule.TaskVo>>> {
  return http.get<ApiResponse<ApplyModule.PageList<ApplyModule.TaskVo>>>(`/admin/task/review/list`,{
    params,
  })
}

export async function searchTodayTaskRecords(params: {
  name?: string
  phone?: string
  status?: string
  page: number
  size: number
  dateTime?: string
}): Promise<ApiResponse<ApplyModule.PageList<ApplyModule.TaskVo>>> {
  return http.get<ApiResponse<ApplyModule.PageList<ApplyModule.TaskVo>>>(`/admin/today/task/review/list`,{
    params,
  })
}

export async function searchOverdueTaskRecords(params: {
  name?: string
  phone?: string
  page: number
  size: number
}): Promise<ApiResponse<ApplyModule.PageList<ApplyModule.TaskVo>>> {
  return http.get<ApiResponse<ApplyModule.PageList<ApplyModule.TaskVo>>>(`/admin/overdue/task/review/list`,{
    params,
  })
}

// 管理员 修改任务状态
export async function modifyTaskRecordStatus(params: {
  taskId: number
  status: string
  actualRepaymentDate?: string
  overdueAmount?: number 
  opCode: string
}): Promise<ApiResponse<ApplyModule.TaskVo>> {
  return http.post<ApiResponse<ApplyModule.TaskVo>>('/admin/update/task/status', params)
}

export const getCosAuth = (): Promise<ApiResponse<ApplyModule.CosAuthVo>> => {
  return http.get<ApiResponse<ApplyModule.CosAuthVo>>('/file/cos/auth')
}


// 管理员删除 申请记录
export const deleteApplyRecord = (id: number, opCode: string): Promise<ApiResponse<string>> => {
  return http.delete<ApiResponse<string>>(`/admin/delete/apply/${id}/${opCode}`)
}

// 管理员删除任务
export const deleteTaskRecord = (id: number, opCode: string): Promise<ApiResponse<string>> =>  {
  return http.delete<ApiResponse<string>>(`/admin/delete/task/${id}/${opCode}`)
}

// 上传文件到腾讯云cos
export const uploadFileToCos = (file: File, fileKey:string): Promise<ApiResponse<string>> => {
  return http.uploadFile<ApiResponse<string>>('/file/cos/upload', file, fileKey)
}

// 获取上传地址
export const getUploadUrl = (params: {fileName:string}): Promise<ApiResponse<ApplyModule.UploadFileUrlConifg>> => {
  return http.get<ApiResponse<ApplyModule.UploadFileUrlConifg>>('/file/upload/url', {
    params,
  })
}

// 后去下载地址
export const getDownloadUrl = (params: {fileId:string}): Promise<ApiResponse<ApplyModule.DownFileUrlResponse>> => {
  return http.get<ApiResponse<ApplyModule.DownFileUrlResponse>>('/file/download/url', {
    params,
  })
}